<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

class RtiMail
{
	protected $_charset = null;
	protected $_headers = null;
	protected $_to = array();
	protected $_cc = array();
	protected $_from = null;
	protected $_subject = null;
	protected $_body_alt = FALSE;
	protected $_body = FALSE;
	protected $_attachments = array();
	protected $_eol = "\r\n";
	public    $has_attachments = FALSE;

	public function __construct($charset = 'utf-8', $eol = "\r\n") 
	{
		$this->_charset = $charset;
		$this->_eol = $eol;
	}

	public function setMessageAlt($body_alt) 
	{
		$this->_body_alt = $body_alt;
	}

	public function setMessage($body) 
	{		
		$this->_body = $body;
	}

	public function setSubject($subject, $encode = FALSE) 
	{
		if ($encode == TRUE) 
		{
			$this->_subject = '=?' . $this->_charset . '?B?' . base64_encode($subject) . '?=';
		} 
		else 
		{
			$this->_subject = $subject;
		}
	}

	public function setFrom($email, $name = null) 
	{
		$this->_from = array('email' => $email, 'name' => $name);
	}

	public function setTo($email, $name = null) 
	{
		if (! empty($email)) 
		{
			$this->_to[] = array($email, $name);
		}
	}
	
	public function setCc($email, $name = null) 
	{
		if (! empty($email)) 
		{
			$this->_cc[] = array($email, $name);
		}
	}
	
	public function setAttachment($filename) 
	{
		if (is_array($filename))
		{
			foreach ($filename as $file) 
			{
				$this->setAttachment($file);
			}
		}
		else if (file_exists($filename) && (is_file($filename))) 
		{
			$data = file_get_contents($filename);
			
			array_push(
				$this->_attachments,
				array(
					'file_name' => basename($filename),
					'file_type' => filetype($filename),
					'file_data' => $data,
				)
			);
			
			$this->has_attachments = TRUE;
		}
	}

	private function getTo($headers = FALSE) 
	{
		$tmp = "";		
		foreach ($this->_to as $to) 
		{
			if (isset($to[0])) 
			{
				$name = (isset($to[1])) ? $to[1] : '';
				
				if (! $headers)
				{
					$tmp .= $to[0] . ', ';
				}
				else
				{
					$tmp .= $name . '<' . $to[0] . '>, ';
				}
			}
		}
		return substr($tmp, 0, -2);
	}
	
	private function getCc($headers = FALSE) 
	{
		$tmp = "";		
		foreach ($this->_cc as $cc) 
		{
			if (isset($cc[0])) 
			{
				$name = (isset($cc[1])) ? $cc[1] : '';
				
				if (! $headers)
				{
					$tmp .= $cc[0] . ', ';
				}
				else
				{
					$tmp .= $name . '<' . $cc[0] . '>, ';
				}
			}
		}
		return substr($tmp, 0, -2);
	}
	
	public function send() 
	{
		// hash
		$random_hash    = 'x' . md5(uniqid('', TRUE)) . 'x';
		$boundary_alt   = 'RtiPHP-alt-' . $random_hash;		
		$boundary_mixed = 'RtiPHP-mixed-' . $random_hash;	
		
		// from
		$from = $this->_from;
		$from_name = (isset($from['name'])) ? $from['name'] : '';
		$from_email_address = (isset($from['email'])) ? $from['email'] : '';
		
		// header
		$header = "From: " . $from_name . " <{$from_email_address}>" . $this->_eol;
		
		$header .= "Reply-To: " . $from_email_address . $this->_eol;
    		$header .= "Return-Path: " . $from_email_address . $this->_eol;
    		$header .= "X-Mailer: RtiPHP\n"; 
		
		// Cc
		$cc = $this->getCc(TRUE);
		if (! empty($cc))
		{
			$header = "Cc: " . $cc . $this->_eol;
		}
		
		$header .= "MIME-Version: 1.0" . $this->_eol;
		$header .= "Content-Type: multipart/mixed; boundary=\"{$boundary_mixed}\"" . $this->_eol . $this->_eol;

		$body = "This is a multi-part message in MIME format." . $this->_eol . $this->_eol;

		$body .= "--{$boundary_mixed}" . $this->_eol;
		$body .= "Content-Type: multipart/alternative; boundary=\"{$boundary_alt}\"" . $this->_eol . $this->_eol;

		if (isset($this->_body_alt)) 
		{
			$body.= "--{$boundary_alt}" . $this->_eol;
			$body.= "Content-Type: text/plain; charset=\"{$this->_charset}\"" . $this->_eol;
			$body.= "Content-Transfer-Encoding: 7bit" . $this->_eol . $this->_eol;
			$body.= $this->_body_alt;
			$body.= $this->_eol . $this->_eol;
		}
		
		if (isset($this->_body)) 
		{
			$body.= "--{$boundary_alt}" . $this->_eol;
			$body.= "Content-Type: text/html; charset=\"{$this->_charset}\"" . $this->_eol;
			$body.= "Content-Transfer-Encoding: 7bit" . $this->_eol . $this->_eol;
			$body.= $this->_body;
			$body.= $this->_eol . $this->_eol;
		}

		$body .= "--{$boundary_alt}--" . $this->_eol . $this->_eol;

		// attachments
		if ($this->has_attachments === TRUE) 
		{
			foreach ($this->_attachments as $attachment) 
			{
				$body .= "--{$boundary_mixed}" . $this->_eol;
				$body .= 'Content-Type: ' . $attachment['file_type'] . '; name="' . $attachment['file_name'] . '"' . $this->_eol;
				$body .= 'Content-Disposition: attachment; filename="' . $attachment['file_name'] . '"' . $this->_eol;
				$body .= "Content-Transfer-Encoding: base64" . $this->_eol . $this->_eol;
				$body .= chunk_split(base64_encode($attachment['file_data'])) . $this->_eol . $this->_eol;
			}
		}

		$body .= "--{$boundary_mixed}--" . $this->_eol . $this->_eol;

		return mail($this->getTo(TRUE), $this->_subject, $body, $header); 
	}	
}

/* End of file RtiMail.php */
/* Location: ./system/helpers/RtiMail.php */
